package com.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class CompanyManager {
    /**
         * Logger for this class
         */
    private static final Logger logger = Logger.getLogger(CompanyManager.class);

    private static ArrayList al = new ArrayList();
    static {

	for (int i = 0; i < 100; i++) {
	    Company p = new Company(i, String.valueOf(3300 + i), "江中集团" + i,
		    "解放西路边69号");
	    al.add(p);
	}
    }

    public DwrPaginate getCompanys(String n, String s, int b, int l)
	    throws ClassNotFoundException {
	DwrPaginate d = new DwrPaginate();
	ArrayList a2 = new ArrayList();
	for (int m = b; m < b + l; m++) {
	    a2.add(al.get(m));
	}
	d.setData(a2.toArray());
	d.setTotalSize(al.size());
	Map m = new HashMap();
	m.put("id", "ID");
	m.put("comNum", "公司编号");
	m.put("comName", "公司名称");
	m.put("comAddress", "公司地址");
	Object o = (Object) com.morik.util.DwrMetaConverter.createMeta(
		"Company", m);
	d.setMetaData(o);

	return d;
    }

    public Company saveCompany(Company c) {
	// 代码省略
	for (int i = 0; i < al.size(); i++) {
	    Company c1 = (Company) al.get(i);
	    if (c.getId() == c1.getId())
		al.set(i, c);
	}
	return c;
    }

    public Company removeCompany(Company c) {
	for (int i = 0; i < al.size(); i++) {
	    Company c1 = (Company) al.get(i);
	    if (c.getId() == c1.getId())
		al.remove(i);
	}
	return c;
    }

    public Company addCompany(Company c) {
	boolean flag = true;
	for (int i = 0; i < al.size(); i++) {
	    Company c1 = (Company) al.get(i);
	    if (c.getComNum().equalsIgnoreCase(c1.getComNum())) {
		flag = false;
		break;
	    }
	}
	if (flag) {
	    c.setId(al.size() + 1);
	    al.add(c);
	}
	return c;
    }
}